<template>
  <div class="p-2">
      <div class="txt-title">
        {{title}}
      </div>
      <div class="time-tit">
        <span class="text-blue">{{author}}</span><span class="text-gray ml-2">{{created_at}}</span>
        <button
          class="shareBtn"
          type="defult"
          open-type="share"
          ></button>
      </div>
      <div class="txt-content">
        <rich-text :nodes="content"></rich-text>
      </div>
  </div>
</template>
<script>
import {integral, views} from '../../utils/utils.js'
export default {
  onShareAppMessage (res) {
    if (res.from === 'button') {
      // 增加积分
      this.sharpePage(this.$root.$mp.query.id, 3)
    } else {
      this.sharpePage(this.$root.$mp.query.id, 4)
    }
    let imgArr = this.content.match(/<img[^>]+>/g)
    return {
      title: this.title,
      imageUrl: imgArr[0],
      path: '/pages/notice_details/main?id=' + this.$root.$mp.query.id
    }
  },
  data () {
    return {
      title: '公告详情',
      author: '我和你酒业',
      created_at: '',
      content: ''
    }
  },
  components: {
  },
  methods: {
    getoption () {
      if (this.$root.$mp.query.id !== '') {
        let _this = this
        wx.request({
          url: this.apiUrl + 'api/article/detail?id=' + this.$root.$mp.query.id,
          success (res) {
            let data = res.data
            if (data.code === 200) {
              _this.author = data.data.author
              _this.title = data.data.title
              _this.created_at = data.data.created_at
              _this.content = data.data.content.replace(/<img/gi, '<img style="width:100%;height:auto;display:block;" ')
            }
          }
        })
      }
    },
    // 积分增加， 更新 分享、浏览 、转发数量，增加积分
    // type:share 更新分享数   views  更新浏览数 forwarding 更新转发数
    getIntegral (type = 2) {
      let _this = this
      let id = _this.$root.$mp.query.id
      // 增加积分
      integral(id, type)
      // 增加统计数量  views
      views(id, 'views')
    },
    // 1.签到 2.阅读文章 3.转发文章 4.分享小程序
    sharpePage (id, type = 3) {
      // 增加积分
      integral(id, type)
      // 增加统计数量 share
      views(id, 'share')
    }
  },
  mounted (option) {
    this.getoption()
    if (wx.getStorageSync('userDtr').length > 0) {
      // 增加积分
      this.getIntegral(2)
    }
  }
}
</script>
<style>
</style>
